package com.wholesale;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.Feedback;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;

public class FeedBackLayout extends LinearLayout implements OnClickListener {   
	private ViewFlipper viewFlipper ;
	private Context context ;
	private View baseView ;
	private Button leftButton ;
	private Button rightButton ;
	private TextView headView ;
	
	private EditText titleText ;
	private EditText commentText ;
	
	
	//  构造函数  
	public FeedBackLayout(Context context,final ViewFlipper viewFlipper){
		super(context);
		this.viewFlipper = viewFlipper ;
		this.context = context ;
		//加载需要的属性，加载方法一的子Layout  
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.feedback, this);  
		leftButton = (Button)baseView.findViewById(R.id.leftBtn);
		leftButton.setText("More");
		leftButton.setOnClickListener(this);
		rightButton = (Button)baseView.findViewById(R.id.rightBtn);
		rightButton.setText("Send");
		rightButton.setOnClickListener(this);
		headView = (TextView)baseView.findViewById(R.id.title);
		headView.setText("New Feedback");
		titleText = (EditText)baseView.findViewById(R.id.title_text);
		commentText = (EditText)baseView.findViewById(R.id.comment_text);
	}
	
	class SendFeedbackAsync extends AsyncTask<Feedback, Integer, Boolean > {
		ProgressDialog pdialog;
		public SendFeedbackAsync(Context context) {
			pdialog = new ProgressDialog(context);
			pdialog.setTitle("Sending...");
			pdialog.setMessage("Please wait...");
			pdialog.setCancelable(true);
			pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
					dialog.cancel();
					SendFeedbackAsync.this.cancel(true);
				}
			});
			pdialog.show();
		}

		@Override
		protected Boolean  doInBackground(Feedback... params) {
			return WebServiceClient.addFeedback(params[0], Constants.loginResponse.getUserToken());
		}

		@Override
		protected void onPostExecute(Boolean result) {
			pdialog.dismiss();
			if(result){
				dialog("Infomation","Send success");
			}else{
				dialog("Error","Send fail");
			}
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}
	
	public void dialog(String title, final String message) {
		AlertDialog.Builder builder = new Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				if(message.indexOf("success") != -1){
					viewFlipper.showPrevious();
					viewFlipper.removeView(FeedBackLayout.this);
				}
			}
		});
		builder.create().show();
	}
	
	
	public void onClick(View v) {
		InputMethodManager imm = (InputMethodManager)((Activity) getContext()).getSystemService(Context.INPUT_METHOD_SERVICE);  
		
		switch (v.getId()) {
		case R.id.leftBtn:
			imm.hideSoftInputFromWindow(leftButton.getWindowToken(), 0);
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			imm.hideSoftInputFromWindow(rightButton.getWindowToken(), 0);
			String title = titleText.getText().toString().trim();
			String comment = commentText.getText().toString().trim();
			if(title.equals("")){
				
			}else if(comment.equals("")){
				
			}else{
				Feedback feedback = new Feedback();
				feedback.setMsgTitle(title);
				feedback.setMsgContent(comment);
				feedback.setUserId(Constants.userInfo.getUserId());
				feedback.setUserName(Constants.userInfo.getUserName());
				SendFeedbackAsync sendFeedbackAsync = new SendFeedbackAsync(context);
				sendFeedbackAsync.execute(feedback);
			}
			break;
		default:
			break;
		}
		
	}
}
